#ifndef     __PROGRESS_H
#define     __PROGRESS_H

#include "../rtx/rtx_os.h"

namespace utils {

template <uint32_t TIME_OUT, uint32_t TICK_MS>
struct Delayer {

public:
    // true timeout
    // false normal
    bool wait_tick() {
        osDelay(TICK_MS);
        m_time_count += TICK_MS;
        return m_time_count >= TIME_OUT;
    }

    inline bool is_after(uint32_t after) {
        return m_time_count >= after;
    } 

    void reset() {
        m_time_count = 0;
    }

private:
    uint32_t m_time_count = 0;
};

struct Progress {
public:
    inline Progress() : m_tick(osKernelGetTickCount()) {
    }

    void reset() {
        m_tick = osKernelGetTickCount();
    }

    bool is_after(uint32_t timeout) const {
        return osKernelGetTickCount() > (m_tick + timeout);
    }

    uint32_t duration() const {
        return osKernelGetTickCount() - m_tick;
    }

private:
    uint32_t m_tick;
};

}

#endif
